package com.wxm.utils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.wxm.exception.TlPayException;
import lombok.Getter;

import java.time.LocalDateTime;

/**
 * <p>
 *      共通返回类
 * </p>
 *
 * @author: wxm
 * @Date: 2023/4/7
 */
@Getter
public class R<T> {
    /**
     * 操作说明
     */
    private String msg = "成功";

    /**
     * 最终数据
     */
    private T data = null;

    /**
     * 状态码 200成功
     */
    private Integer code = 200;

    /**
     * 操作时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private final LocalDateTime timestamp = LocalDateTime.now();

    private R(){}
    private R(Integer code, String msg){
        this.code = code;
        this.msg = msg;
    }
    private R(Integer code, String msg,T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private R(T data){
        this.data = data;
    }
    public static R<String> ok(){
        return new R<>();
    }
    public static <T> R<T> ok(T data){
        return new R<T>(data);
    }

    public static R<String> error(String msg){
        return new R<>(500, msg);
    }
    public static R<String> error(Integer code,String msg){
        return new R<>(code, msg);
    }
    public static R<String> error(String msg,Exception e){
        return new R<>(500,msg + "： " + e.getMessage());
    }

    public static void exception(String msg){
        throw new TlPayException(500,msg);
    }
}
